#include "mainwindow.h"
#include "qrcode.h"

#define help_CHN "帮助：\n使用您的账户获取板凳提取码并在左侧输入，如需归还，请点击上方归还按钮"

void MainWindow::system(std::string str) {
    std::cout<<str;
}

void MainWindow::initGPIO() {
    /*
    GPIO_export = fopen("/sys/class/gpio/export","w");
    fprintf(GPIO_export,"23\n");
    fprintf(GPIO_export,"24\n");
    fprintf(GPIO_export,"25\n");

    light_1_dir = fopen("/sys/class/gpio/gpio23/direction","w");
    light_2_dir = fopen("/sys/class/gpio/gpio24/direction","w");
    light_3_dir = fopen("/sys/class/gpio/gpio25/direction","w");
    if (light_1_dir == NULL || light_2_dir == NULL || light_3_dir == NULL) {
        printf("Failed open file\n");
        return ;
    }
    fprintf(light_1_dir,"out\n");
    fprintf(light_2_dir,"out\n");
    fprintf(light_3_dir,"out\n");
    light_1 = fopen("/sys/class/gpio/gpio23/value","w");
    light_2 = fopen("/sys/class/gpio/gpio24/value","w");
    light_3 = fopen("/sys/class/gpio/gpio25/value","w");
    fprintf(light_1,"1\n");
    fprintf(light_2,"1\n");
    fprintf(light_3,"1\n");
    */
    system("echo 23 > /sys/class/gpio/gpiochip0/export");
    system("echo 24 > /sys/class/gpio/gpiochip0/export");
    system("echo 25 > /sys/class/gpio/gpiochip0/export");
    system("echo out > /sys/class/gpio/gpiochip0/gpio23/direction");
    system("echo out > /sys/class/gpio/gpiochip0/gpio24/direction");
    system("echo out > /sys/class/gpio/gpiochip0/gpio25/direction");
    system("echo 1 > /sys/class/gpio/gpiochip0/gpio23/value");
    system("echo 1 > /sys/class/gpio/gpiochip0/gpio24/value");
    system("echo 1 > /sys/class/gpio/gpiochip0/gpio25/value");
    printf("DEBUG:successfully inited\n");

    //echo 24 >/sys/class/gpio/export
    //echo out >/sys/class/gpio/gpio24/direction
    //echo 1 >/sys/class/gpio/gpio24/value

}
void MainWindow::initSignalSlots() {
    connect(zeroBtn,SIGNAL(clicked(bool)),this,SLOT(setPasswordTextZero()));
    connect(oneBtn,SIGNAL(clicked(bool)),this,SLOT(setPasswordTextOne()));
    connect(twoBtn,SIGNAL(clicked(bool)),this,SLOT(setPasswordTextTwo()));
    connect(threeBtn,SIGNAL(clicked(bool)),this,SLOT(setPasswordTextThree()));
    connect(fourBtn,SIGNAL(clicked(bool)),this,SLOT(setPasswordTextFour()));
    connect(fiveBtn,SIGNAL(clicked(bool)),this,SLOT(setPasswordTextFive()));
    connect(sixBtn,SIGNAL(clicked(bool)),this,SLOT(setPasswordTextSix()));
    connect(sevenBtn,SIGNAL(clicked(bool)),this,SLOT(setPasswordTextSeven()));
    connect(eightBtn,SIGNAL(clicked(bool)),this,SLOT(setPasswordTextEight()));
    connect(nineBtn,SIGNAL(clicked(bool)),this,SLOT(setPasswordTextNine()));
    connect(verifyBtn,SIGNAL(clicked(bool)),this,SLOT(verifyPassword()));
    connect(clearBtn,SIGNAL(clicked(bool)),this,SLOT(clearPassword()));
    connect(returnBtn,SIGNAL(clicked(bool)),this,SLOT(return_product()));
}

void MainWindow::initUI() {
    zeroBtn = new QPushButton(tr("0"));
    oneBtn = new QPushButton(tr("1"));
    twoBtn = new QPushButton(tr("2"));
    threeBtn = new QPushButton(tr("3"));
    fourBtn = new QPushButton(tr("4"));
    fiveBtn = new QPushButton(tr("5"));
    sixBtn = new QPushButton(tr("6"));
    sevenBtn = new QPushButton(tr("7"));
    eightBtn = new QPushButton(tr("8"));
    nineBtn = new QPushButton(tr("9"));
    verifyBtn = new QPushButton(tr("完成"));
    clearBtn = new QPushButton(tr("清除"));
    returnBtn = new QPushButton(tr("归还"));
    txtEdit = new QPlainTextEdit;
    txtEdit->setPlaceholderText("输入提取码");
    qrDisplay = new QLabel();
    QString qrcodePath = "/Users/cgk/Documents/CPP/totp-qt/qrcode.png";
    std::string segno = "/opt/local/bin/segno-3.8 -v 2 --scale=3 --output=/Users/cgk/Documents/CPP/totp-qt/qrcode.png \"";
    segno += "1";
    segno += "\" ";
    std::system(segno.c_str());

    qrcode = new QImage;
    qrcode->load(qrcodePath);
    qrDisplay->setPixmap(QPixmap::fromImage(*qrcode));
    qrDisplay->setFixedHeight(200);
    qrDisplay->setFixedWidth(200);

    helpInstruction = new QPlainTextEdit;
    helpInstruction->setPlainText(help_CHN);
    helpInstruction->setFixedWidth(200);
    QFont *helpFont = new QFont;
    helpFont->setFamily("Arial");
    helpFont->setBold(true);
    helpFont->setPointSize(15);
    helpInstruction->setFont(*helpFont);

    zeroBtn->setMinimumHeight(100);
    zeroBtn->setMaximumHeight(100);
    zeroBtn->setMinimumWidth(100);
    zeroBtn->setMaximumWidth(100);
    zeroBtn->setStyleSheet("QPushButton:pressed { \
                             background-color: rgb(224, 0, 0);        \
                             border-style: inset;                     \
                             }");

    QFont tmp = zeroBtn->font();
    tmp.setPointSize(50);
    zeroBtn->setFont(tmp);
    oneBtn->setFont(tmp);
    twoBtn->setFont(tmp);
    threeBtn->setFont(tmp);
    fourBtn->setFont(tmp);
    fiveBtn->setFont(tmp);
    sixBtn->setFont(tmp);
    sevenBtn->setFont(tmp);
    eightBtn->setFont(tmp);
    nineBtn->setFont(tmp);
    tmp.setPointSize(25);
    verifyBtn->setFont(tmp);
    clearBtn->setFont(tmp);
    returnBtn->setFont(tmp);


    oneBtn->setMinimumHeight(100);
    oneBtn->setMaximumHeight(100);
    oneBtn->setMinimumWidth(100);
    oneBtn->setMaximumWidth(100);
    oneBtn->setStyleSheet("QPushButton:pressed { \
                          background-color: rgb(60, 122, 219);        \
                          border-style: inset;                     \
                          }");

    twoBtn->setMinimumHeight(100);
    twoBtn->setMaximumHeight(100);
    twoBtn->setMinimumWidth(100);
    twoBtn->setMaximumWidth(100);
    twoBtn->setStyleSheet("QPushButton:pressed { \
                          background-color: rgb(110, 206, 26);        \
                          border-style: inset;                     \
                          }");

    threeBtn->setMinimumHeight(100);
    threeBtn->setMaximumHeight(100);
    threeBtn->setMinimumWidth(100);
    threeBtn->setMaximumWidth(100);
    threeBtn->setStyleSheet("QPushButton:pressed { \
                            background-color: rgb(241, 149, 28);        \
                            border-style: inset;                     \
                            }");

    fourBtn->setMinimumHeight(100);
    fourBtn->setMaximumHeight(100);
    fourBtn->setMinimumWidth(100);
    fourBtn->setMaximumWidth(100);
    fourBtn->setStyleSheet("QPushButton:pressed { \
                           background-color: rgb(123, 0, 253);        \
                           border-style: inset;                     \
                           }");

    fiveBtn->setMinimumHeight(100);
    fiveBtn->setMaximumHeight(100);
    fiveBtn->setMinimumWidth(100);
    fiveBtn->setMaximumWidth(100);

    sixBtn->setMinimumHeight(100);
    sixBtn->setMaximumHeight(100);
    sixBtn->setMinimumWidth(100);
    sixBtn->setMaximumWidth(100);

    sevenBtn->setMinimumHeight(100);
    sevenBtn->setMaximumHeight(100);
    sevenBtn->setMinimumWidth(100);
    sevenBtn->setMaximumWidth(100);

    eightBtn->setMinimumHeight(100);
    eightBtn->setMaximumHeight(100);
    eightBtn->setMinimumWidth(100);
    eightBtn->setMaximumWidth(100);

    nineBtn->setMinimumHeight(100);
    nineBtn->setMaximumHeight(100);
    nineBtn->setMinimumWidth(100);
    nineBtn->setMaximumWidth(100);

    verifyBtn->setMinimumHeight(100);
    verifyBtn->setMaximumHeight(100);
    verifyBtn->setMinimumWidth(100);
    verifyBtn->setMaximumWidth(100);

    clearBtn->setMinimumHeight(100);
    clearBtn->setMaximumHeight(100);
    clearBtn->setMinimumWidth(100);
    clearBtn->setMaximumWidth(100);

    returnBtn->setMinimumHeight(100);
    returnBtn->setMaximumHeight(100);
    returnBtn->setMinimumWidth(100);
    returnBtn->setMaximumWidth(100);
    returnBtn->setStyleSheet("QPushButton:pressed { \
                          background-color: rgb(60, 122, 219);        \
                          border-style: inset;                     \
                          }");

    txtEdit->setReadOnly(true);


    QHBoxLayout *LineOne, *LineTwo, *LineThree, *LineFour, *Display;
    LineOne = new QHBoxLayout;
    LineTwo = new QHBoxLayout;
    LineThree = new QHBoxLayout;
    LineFour = new QHBoxLayout;

    LineFour->addWidget(zeroBtn);
    //LineFour->addSpacing(100);
    LineFour->addWidget(clearBtn);
    LineFour->addWidget(verifyBtn);


    LineOne->addWidget(oneBtn);
    LineOne->addWidget(twoBtn);
    LineOne->addWidget(threeBtn);

    LineTwo->addWidget(fourBtn);
    LineTwo->addWidget(fiveBtn);
    LineTwo->addWidget(sixBtn);

    LineThree->addWidget(sevenBtn);
    LineThree->addWidget(eightBtn);
    LineThree->addWidget(nineBtn);

    QHBoxLayout *Return_and_Qr;
    Return_and_Qr = new QHBoxLayout;
    Return_and_Qr->addWidget(returnBtn);
    Return_and_Qr->addWidget(qrDisplay);
    Return_and_Qr->setSpacing(0);

    VLay = new QVBoxLayout;
    VLay->addWidget(txtEdit);
    VLay->addLayout(LineOne);
    VLay->addLayout(LineTwo);
    VLay->addLayout(LineThree);
    VLay->addLayout(LineFour);
    VLay->addStretch();
    //VLay->addWidget(verifyBtn);
    // more to be added
    VLay2 = new QVBoxLayout;
    VLay2->addLayout(Return_and_Qr);
    VLay2->addWidget(helpInstruction);

    Display = new QHBoxLayout;
    Display->addLayout(VLay);
    Display->addStretch();
    Display->addLayout(VLay2);

    auto central = new QWidget;
        central->setLayout(Display);

        setCentralWidget(central);
}

void MainWindow::genQRCode(std::string str) {
    if (this->qrcode_buffer != NULL) {
        //free(this->qrcode_buffer);
    }
    QRCode qrcode;
    this->qrcode_buffer = (char *)malloc(qrcode_getBufferSize(LOCK_VERSION) + 1);
    qrcode_initText(&qrcode, (uint8_t *)this->qrcode_buffer, 3, ECC_LOW, str.c_str());
    if (img_buffer != NULL) {
        //free(img_buffer);
    }
    img_buffer = (char *)malloc(qrcode.size * (qrcode.size+1));
    for (uint8_t x = 0; x < qrcode.size; ++x) {
        for (uint8_t y =  0; y < qrcode.size; ++y) {
            if (qrcode_getModule(&qrcode, x, y)) {
                img_buffer[x * (qrcode.size + 1) + y] = '*';
            } else {
                img_buffer[x * (qrcode.size + 1) + y] = ' ';
            }
        }
        img_buffer[x * (qrcode.size + 1) + qrcode.size] = '\n';
    }
    qrcode_buffer[qrcode_size * (qrcode_size + 1)] = 0;
    qrcode_size = qrcode.size;
}

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    qrcode_buffer = NULL;
    cotp_error_t err_t;
    totp = get_totp ("OF3WK4TUPE", 6, 120, SHA256, &err_t);
    return_totp = get_totp ("OF3WK4TUPE", 6, 120, SHA256, &err_t);
    printf("DEBUG: %s\n", totp);
    initUI();
    initSignalSlots();
    initGPIO();
    setWindowTitle("UCAS Shareseat Confidential");
}

MainWindow::~MainWindow()
{

}
